class BoxItem < ActiveRecord::Base
  belongs_to :user
  belongs_to :message
  belongs_to :thread, :class_name => 'Message', :foreign_key => 'thread_id'
  
  def read?
    read_at
  end

  def purge
    if self.inbox_deleted && self.outbox_deleted
      self.destroy
      self.message.purge
    end
  end
end

class InboxItem < BoxItem
  def self.mark_read(user, id)
    InboxItem.update_all ["read_at = ?", Time.now], :user_id => user.id, :thread_id => id
  end
  
  def self.mark_unread(user, id)
    InboxItem.update_all 'read_at = NULL', :user_id => user.id, :thread_id => id
  end
  
  def self.mark_read_all(user, ids)
    InboxItem.update_all ['read_at = ?', Time.now], :user_id => user.id, :thread_id => ids
  end
  
  def self.mark_unread_all(user, ids)
    InboxItem.update_all ['read_at = NULL'], :user_id => user.id, :thread_id => ids
  end
end

class OutboxItem < BoxItem
  
end